home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Documentation / Tech Notes & Articles / Recipes / Storage / Storage Unit < prev   
Encoding:
Text File  |  1995-07-11  |  5.7 KB  |  231 lines  |  [TEXT/ttxt]

  1. OpenDocâ„¢ Recipes
  2.  
  3.  
  4. Storage Unit Manipulation
  5. By The OpenDoc Design Team
  6. July 11, 1995.
  7.  
  8.  
  9. © 1993-1995  Apple Computer, Inc. All Rights Reserved.
  10. Apple, the Apple logo, and Macintosh are registered trademarks of Apple Computer, Inc.
  11. Mac and OpenDoc are trademarks of Apple Computer, Inc.
  12.  
  13. Changes since DR2
  14.  
  15. 1) Refcounting methods name change.
  16. 2) Fixed parameter passing errors of SetValue and GetValue.
  17.  
  18. About this document:
  19.  
  20. This document describes the basic usage of ODStorageUnit with actual examples and recipes. It is intended to supplement the Class Documentation. For detailed description of the ODStorageUnit methods, please refer to the Class Documentation.
  21.  
  22. Recipes / Examples
  23.  
  24. Recipe 1: Given a ODStorageUnit, create multiple Properties and Values.
  25.  
  26. su->AddProperty(ev, kODPropContents);
  27. su->AddValue(ev, kMyKind);
  28. su->AddValue(ev, kKindTEXT);
  29. su->AddValue(ev, kKindPICT);
  30.  
  31. su->AddProperty(ev, kPropAnnotations);
  32. su->AddValue(ev, kMyAnnotationsType);
  33.  
  34. Recipe 2: Given the ODStorageUnit in Recipe 1, set the focus to a certain Value.
  35.  
  36.     There is more than one way to get to the value:
  37.  
  38.     1)    Focus the ODStorageUnit to the Value using the known Property and Type.
  39.  
  40. su->Focus(ev,
  41.             kODPropContents,        // Property Name
  42.             kODPosUndefined,
  43.             kMyKind,                                        // Value Type        
  44.             0,
  45.             kODPosUndefined);
  46.  
  47.     2)    Focus the ODStorageUnit to the Value using the known Property and Value Index.
  48.  
  49. su->Focus(ev,
  50.             kODPropContents,        // Property Name
  51.             kODPosUndefined,
  52.             kODNULL,
  53.             1,                                                                // Value Index
  54.             kODPosUndefined);
  55.  
  56.     3)    Focus the ODStorageUnit to the Value using the known Property and relative             Position.
  57.  
  58. su->Focus(ev,
  59.             kODPropContents,        // Property Name
  60.             kODPosUndefined,
  61.             kODNULL,
  62.             0,
  63.             kODPosFirstSib);        // Position code
  64.  
  65.     4)    Focus the ODStorageUnit to the Value using a ODStorageUnitCursor. This method is        especially useful when one needs to focus to a Property or a Value frequently.
  66.  
  67. There are two ways to create a cursor:
  68. - by specifying the focus explicitly.
  69.         ODStorageUnitCursor* cursor = su->CreateCursor(ev, kODPropContents, kMyKind, 0);
  70. - using the focus of a Storage Unit:
  71.         ODStorageUnitCursor* cursor = su->CreateCursorWithFocus(ev);
  72.  
  73. One can then focus a storage unit using the created cursor:
  74.         su->FocusWithCursor(ev, cursor);
  75.  
  76. Recipe 3: Given the ODStorageUnit from Recipe 1, iterate through all the Properties.
  77.  
  78. // Unfocus storage unit
  79.  
  80. su->Focus(ev,
  81.             kODNULL,
  82.             kODPosAll,                                // Position Code
  83.             kODNULL,
  84.             0,
  85.             kODPosAll);                            // Position Code
  86.  
  87. // Get the number of Properties.
  88.  
  89. ODULong numProperties = su->CountProperties(ev);
  90.  
  91. // Iterate through the Properties
  92.  
  93. for (i = 1; i <= numProperties; i++)
  94. {
  95.  
  96.     su->Focus(ev,
  97.                 kODNULL,
  98.                 kODPosNextSib,            // Position Code
  99.                 kODNULL,
  100.                 0,
  101.                 kODPosUndefined);
  102. }
  103.  
  104. Recipe 4:     Given the ODStorageUnit from Recipe 1, iterate through all the Values in kODPropContents Property.
  105.  
  106. // focus to the desired Property
  107.  
  108. su->Focus(ev,
  109.             kODPropContents,        // Property Name
  110.             kODPosUndefined,
  111.             kODNULL,
  112.             0,
  113.             kODPosAll);                            // Position code
  114.  
  115. // Get the number of Values.
  116.  
  117. ODULong numValues = su->CountValues(ev);
  118.  
  119. // Iterate through the Values
  120.  
  121. for (i = 1; i <= numValues; i++)
  122. {
  123.  
  124.     su->Focus(ev,
  125.                 kODNULL,
  126.                 kODPosSame,        // Position code
  127.                 kODNULL,
  128.                 0,
  129.                 kODPosNextSib);    // Position code
  130. }
  131.  
  132. Recipe 5:    Given the ODStorageUnit from Recipe 1, remove the kPropAnnotationsProperty.
  133.  
  134. // focus to the desired Property
  135.  
  136. su->Focus(ev,
  137.             kPropAnnotations,
  138.             kODPosUndefined,
  139.             kODNULL,
  140.             0,
  141.             kODPosAll);
  142.  
  143. // remove the Property
  144.  
  145. su->Remove(ev);
  146.     
  147. Recipe 6:    Given the ODStorageUnit from Recipe 1, remove the kKindPICT Value in kODPropContents Property.
  148.  
  149. // focus to the desired Value
  150.  
  151. su->Focus(ev,
  152.             kODPropContents,
  153.             kODPosUndefined,
  154.             kODNULL,
  155.             kKindPICT,
  156.             kODPosUndefined);
  157.  
  158. // remove the Value
  159.  
  160. su->Remove(ev);
  161.     
  162. Recipe 7:    Given the focused ODStorageUnit from Recipe 2, manipulate the data in the Value. One can think of a Value as a stream.  In order for Storage Units to work in a distributed environment, all the data passed in or out through the Storage Unit API are in ODByteArray. The following is the structure of an ODByteArray (from the IDL emitter):
  163.  
  164. typedef struct {
  165.     unsigned long _maximum;
  166.     unsigned long _length;
  167.     octet *_buffer;
  168. } _IDL_SEQUENCE_octet;
  169. typedef _IDL_SEQUENCE_octet ODByteArray;
  170.  
  171. _maximum contains the size of the memory block pointed to by _buffer.
  172. _length contains the number of bytes (i.e., octets) of relevant data in the memory block pointed to by _buffer. This must be smaller than or equal to _maximum.
  173. _buffer contains a pointer to a memory block whose size is reflected in _maximum.
  174.  
  175.  
  176.     To add data at a particular position:
  177.  
  178. su->SetOffset(ev, desiredPosition);
  179. // Set up byte array
  180. ODByteArray ba;
  181. ba._length = length; // length of data to be written
  182. ba._maximum = maximum; // maximum size of buffer
  183. ba._buffer = buffer; // pointer to the data
  184. su->SetValue(ev, &ba);
  185.  
  186.     
  187.     To add data at the current offest:
  188.  
  189. // No SetOffset is needed. Just call SetValue.
  190. su->SetValue(ev, &ba);
  191.  
  192.  
  193.     To insert data at a particular position:
  194.  
  195. su->SetOffset(ev, desiredPosition);
  196. su->InsertValue(ev, &ba);
  197.  
  198.  
  199.     To append data at the end of a Value:
  200.  
  201. // Get the length of the value
  202. ODULong offset = su->GetSize(ev);
  203. // Set the offset to the end of the value.
  204. su->SetOffset(ev, offset);
  205. su->SetValue(ev, &ba);
  206.  
  207.  
  208.     To remove data from a particular position:
  209.  
  210. su->SetOffset(ev, offset);
  211. su->DeleteValue(ev, length);
  212.  
  213.  
  214. To get data from a particular position:
  215.  
  216. // Get to the right position
  217. su->SetOffset(ev, offset);
  218.  
  219. // Set up byte array. There is really nothing to do here 
  220. // because the fields of ODByteArray are going to be filled out by
  221. // GetValue.
  222. ODByteArray ba;
  223.  
  224. // Get the data.
  225. su->GetValue(ev, desiredLength, &ba);
  226.  
  227. // Dispose the buffer created by GetValue when done.
  228. ODDisposePtr(ba._buffer);
  229.  
  230.  
  231.